-- Files to send to the client
AddCSLuaFile( 'cl_init.lua' )
AddCSLuaFile( 'shared.lua' )
AddCSLuaFile( 'cl_stathud.lua' )
AddCSLuaFile( 'cl_statmenu.lua' )
AddCSLuaFile( 'cl_usermessages.lua' )
AddCSLuaFile( 'cl_wavehud.lua' )
AddCSLuaFile( 'cl_powercorehud.lua' )
AddCSLuaFile( 'cl_adminpanel.lua' )

-- Server Should run Shared...
include( 'shared.lua' )
include( 'sv_buyupgrade.lua' )
include( 'sv_util.lua' )
include( 'sv_wavemanager.lua' )
include( 'sv_statmanager.lua' )
include( 'sv_admincommands.lua' )


--------------
-- GAMEMODE --
--------------
function GM:PlayerInitialSpawn( ply )
	-- They start off alive!
    ply:SetTeam( TEAM_LIVING )
	
	-- Stats
	local level = ply:GetPData("inv_level")
	local xp = ply:GetPData("inv_xp")
	local statpoints = ply:GetPData("inv_statpoints")
	local money = ply:GetPData("inv_money")
	local weaponspeed = ply:GetPData("inv_weaponspeed")
	local weapondamage = ply:GetPData("inv_weapondamage")
	local speed = ply:GetPData("inv_speed")
	local clipsize = ply:GetPData("inv_clipsize")
	local maxhealth = ply:GetPData("inv_maxhealth")
	local damagetaken = ply:GetPData("inv_damagetaken")
	local ownedbuyables = glon.decode(ply:GetPData("inv_ownedbuyables"))
	local jump = ply:GetPData("inv_jump")
	local spread = ply:GetPData("inv_spread")
	
	-- Simple check if it is a new player, if so, reset their stats.
	if level == nil then
		print("Player has no stats!, I will create them now!")
		inv_resetstats( ply, nil, nil )
	else
		print("Player has stats, loading them now!")
		ply:SetStats(level, xp, statpoints, money, weaponspeed, weapondamage, speed, clipsize, maxhealth, damagetaken, ownedbuyables, jump, spread)
	end
	
	-- Join messages!
	for k, v in pairs(INV_JOINMESSAGES) do
		SendMessageToPlayerChat(ply, v)
	end
	
end

function GM:PlayerSelectSpawn( pl )
	if(pl:Team() == TEAM_LIVING) then
		local possibleplayerspawns = ents.FindByClass("player_spawn")
		local randomspawn = math.random(#possibleplayerspawns)
		return possibleplayerspawns[randomspawn]
	end
	if(pl:Team() == TEAM_SPEC) then
		local possibleplayerspawns = ents.FindByClass("spectator_spawn")
		local randomspawn = math.random(#possibleplayerspawns)
		return possibleplayerspawns[randomspawn]
	end
end

-- Will strip weapons and re-give everything
function GivePlayerOwnedStuff(ply)
	
	ply:StripWeapons()

	local ownedbuyables = glon.decode(ply:GetOwnedBuyables())
	
	for i=1, table.Count(ownedbuyables) do
		local ID = ownedbuyables[i]
		-- Some things are not weapons and they need handling differently
		if(INV_BUYABLES[ID][5] == "weapon") then
			ply:Give(INV_BUYABLES[ID][3])
		end
		if(INV_BUYABLES[ID][5] == "function") then
			INV_BUYABLES[ID][3]()
		end
	end	
end
 
function DoPlayerUpgrades( ply )
		ply:SetMaxHealth(ply:GetMaxHealth())
		ply:SetHealth(ply:GetMaxHealth())
		
		ply:SetWalkSpeed(INV_BASESPEEDWALK * (ply:GetSpeed() / 100))
		ply:SetRunSpeed(INV_BASESPEEDRUN * (ply:GetSpeed() / 100))
		
		ply:SetJumpPower(INV_BASEJUMP * (ply:GetJump() / 100))
end
 
function GM:PlayerLoadout( ply )
    if ply:Team() == TEAM_LIVING then
		DoPlayerUpgrades(ply)
		GivePlayerOwnedStuff(ply)
    end
	
	if ply:Team() == TEAM_SPEC then
		DoPlayerUpgrades(ply)
	end
end

function playerDies( victim, weapon, killer )
	victim:SetTeam(TEAM_SPEC)
	
	if(team.NumPlayers(TEAM_LIVING) == 0) then
		GameOver()
	end
end
hook.Add( "PlayerDeath", "inv_playerDeath", playerDies )


function GM:PlayerShouldTakeDamage( ply, attacker )
		-- Players cant hurt players
        if ( attacker:IsValid() and attacker:IsPlayer() and !INV_PLAYERSHURTPLAYERS) then return false end
		-- Players aren't hurt by their turrets!
		if ( attacker:GetClass() == "npc_turret_floor" ) then return false end
        return true
end

 
local function ShouldCollideHook( ent1, ent2 )
	if ( ent1:IsPlayer() and ent2:IsPlayer() ) then
		return INV_PLAYERSCOLLIDE
	end 
end
hook.Add( "ShouldCollide", "ShouldCollideHook", ShouldCollideHook )